package com.rainy.guoran.business.manager.controller;

import com.rainy.guoran.business.manager.controller.vo.UserRespVO;
import com.rainy.guoran.business.manager.controller.vo.UserSaveReqVO;
import com.rainy.guoran.business.manager.entity.CommonResult;
import com.rainy.guoran.business.manager.entity.User;
import com.rainy.guoran.business.manager.services.UserService;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.rainy.guoran.business.manager.entity.CommonResult.success;

@AllArgsConstructor
@RestController
@RequestMapping("/system/user")
@Tag(
        name = "UserController",
        description = "用户管理接口",
        externalDocs = @ExternalDocumentation(
                description = "这是一个接口文档介绍",
                url = "https://rainy.guoran.com/"))
public class UserController {

    private final UserService userService;

    @PostMapping("/create")
    @Operation(summary = "新增用户")
    public CommonResult<Long> createUser(@Valid @RequestBody UserSaveReqVO reqVO) {
        Long id = userService.insertUser(reqVO);
        return success(id);
    }

    @PutMapping("update")
    @Operation(summary = "修改用户")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody UserSaveReqVO reqVO) {
        userService.updateUser(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        userService.deleteUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserRespVO> getUser(@RequestParam("id") Long id) {
        User user = userService.selectUserById(id);
        if (user == null) {
            return success(null);
        }
        UserRespVO userRespVO = new UserRespVO();
        BeanUtils.copyProperties(user, userRespVO);
        return success(userRespVO);
    }

}
